// datastore.cpp : Defines the entry point for the console application.
//

#include ".\datastore.h"
using namespace std;

int CoreTest()
{
	try {
		datastore2 DS;
		vector<char*> tables;
		tables.push_back("names");
		tables.push_back("sales");
		//DS.getColumns(tables);
		
		//Test for delete
		dbcore Core;
		vector<char*> ids;
		ids.push_back("16");
		ids.push_back("9");
		ids.push_back("10");
//		Core.Delete("sales","+sale_id", ids);
		
		vector<char*> idColumns;
		idColumns.push_back("9");
		idColumns.push_back("10");
		idColumns.push_back("15");
		//Core.Update("sales", "+sale_id", idColumns, "price", "500");
		DS.displayResults(DS.getDataFromTable("sales"));

		std::cout<<Core.getColumnType("sales","price")<<"\n";
		std::cout<<Core.getColumnTypeString("sales","+sale_id");
		//Not work
		//DS.getTables();

		//std::cout<<result.back();
		//DS.dropTable("r");
		/*datastore2 DS;
		dbcore Core;
		vector < vector <char*> > joinVector;
		vector < vector <char*> > result2;
		
		vector< vector<char*> > test;
		test = DS.getColumns("names");
		std::cout<<"Size: "<<test.at(1).size();
		
		vector<char*> columns;
		columns.push_back("id");
		columns.push_back("testRow");
		
		vector<char*> columnsTypes;
		columnsTypes.push_back("int");
		columnsTypes.push_back("varchar");
		*/
		
		//DS.createTable("test", columns, columnsTypes);
		//DS.dropTable("test");
		//testJoin.push_back("names");
		//testJoin.push_back("sales");
		
		//joinVector = Core.superJoin(testJoin, result2);
		//result2 = Core.Select("sales");
		//std::string st = Core.vectorToString(result2);
		//DS.displayResults(result1);
		//testJoin.push_back("names");
	
		//result2 = Core.Join(DS.getDataFromTable("sales"), DS.getDataFromTable("names"));

		
		//DS.displayResults(result1);
		//std::cout<< result1.size();
		//vector<char*> namesOfColumns;
		//namesOfColumns.push_back("name");
		//namesOfColumns.push_back("price");
		

		//result 1 = join "names" and "sales" table where id2 = sale_id
		//vector < vector <char*> > result1;
	//	result1 =Core.Select(joinVector, "id2", "sale_id");
	//	result1 = Core.Join("names", "sales");
		//Select only namesOfColumns columns from result1
	//	DS.displayResults(result1);
		
		//DS.displayResults(Core.Select("table1"));
		//result1 = Core("table1");
		
		//result2 = Core.Select("table1");
		/*for (int i = 0; i < result2.size(); i++) {
			cout<<Core.countVector(result2[i])<<"\t";
		}*/
		
		//!!! example with join data !!!
		/*
		result2 = Core.Join("table1", "table2");
		cout<<"Select:\n";
		vector<char*> nameOfColumn;
		vector<char*> operand;
		vector<char*> value;
		vector<char*> insert;
		nameOfColumn.push_back("new_name");
		operand.push_back("=");
		value.push_back("Petrov");
		nameOfColumn.push_back("new_age");
		operand.push_back("=");
		value.push_back("34");
		 
		DS.displayResults(Core.Select(result2,nameOfColumn,operand,value));
		*/
		/*
		vector< vector<char*> > joinVector = Core.Join("names", "sale");
		DS.displayResults(joinVector);
		std::cout<<"JOIN RESULT:\n";
		DS.displayResults(Core.Select(joinVector,"id2", "sale_id"));
		*/
 		} catch(dbmsException e ) {
		std::cout<<"Error: "<< e.getMessage();
	} 
	//!!! Example with insert data !!!
	/*	try {
			dbcore Core;
			datastore2 DS;
			vector <char*> insertVector; 
			insertVector.push_back("20");
			insertVector.push_back("145");
			Core.Insert("sales", insertVector);
			DS.displayResults(Core.Select("sales"));
		} catch(dbmsException e) {
			std::cout<<e.getMessage();
		}
	
	*/
	
	//!!! Example with delete data from tables with some condition(s) !!!
	/*
	cout<<"Delete:\n";
	datastore2 DS;
	dbcore Core;
	vector<char*> nameOfColumn;
	vector<char*> operand;
	vector<char*> value;
	vector<char*> insert;
	nameOfColumn.push_back("sale_id");
	operand.push_back("=");
	value.push_back("6");
	Core.Delete("sales",nameOfColumn,operand,value);
	DS.displayResults(Core.Select("sales"));
	*/
	
	/*
	!!! Example with update data !!!
	*/
	/*
	datastore2 DS;
	dbcore Core;
	char* nameOfColumn = "sale_id";
	char* operand = "=";
	char* value = "2";
	char* nameOfUpdateColumn = "price";
	char* newValue = "600";
	DS.displayResults(DS.getDataFromTable("sales"));
	Core.Update("sales", nameOfColumn, operand, value, nameOfUpdateColumn, newValue);
	DS.displayResults(DS.getDataFromTable("sales"));
	*/

	/*
	
	!!! Example with select all data from table !!!

	result1 = Core.Select("table1");
	DS.displayResults(result1);
	*/	
		
	/*

	!!! Example with select data from tables with some condition(s) !!!
	
	cout<<"Select:\n";
	vector<char*> nameOfColumn;
	vector<char*> operand;
	vector<char*> value;
	vector<char*> insert;
	nameOfColumn.push_back("new_name");
	operand.push_back("=");
	value.push_back("Petrov");
	nameOfColumn.push_back("new_age");
	operand.push_back("=");
	value.push_back("23");
	result2 = Core.Select("table1",nameOfColumn,operand,value);
	DS.displayResults(result2);
	*/

	/*
	nameOfColumn.push_back("name");
	operand.push_back("=");
	value.push_back("Petrov");
	result2 = Core.Select("table1",nameOfColumn,operand,value);
	DS.displayResults(result2);
	insert.push_back("13");
	insert.push_back("Stepanov");
	insert.push_back("24");
	Core.Insert("table1",insert);
	result1.clear();
	result1 = DS.getDataFromTable("table1");
	DS.displayResults(result1);

	//DS.getTables("database1.txt");	
	//DS.setDataToTable("table2",result);
	*/
	return 0;
}

int InterpTest()
{
	cText *t = new cText();
	cToken *tok = new cToken();
//	cExpression *ex = new cExpression();
//	ex->readExpr(t, tok);
	cTree *tr = new cTree();
	tr->readTree(t, tok);
	tr->execTree();

	return 0;
}


int main(int argc, char* argv[])
{
	//SetConsoleCP(1251);
//	CoreTest();
//	InterpTest();
	startServer();
}
